package com.viphuli.app.tool.fragment;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import butterknife.Bind;

import com.loopj.android.http.RequestParams;
import com.viphuli.app.tool.R;
import com.viphuli.app.tool.adapter.HolidayLeaveAdapter;
import com.viphuli.app.tool.bean.part.HolidayLeave;
import com.viphuli.app.tool.common.AccessTokenKeeper;
import com.viphuli.app.tool.common.MyPageHelper;
import com.viphuli.app.tool.fragment.DayHourPickerDialogFragment.DayHourClickListener;
import com.viphuli.app.tool.http.ApiRequest;

/**
 * 假期余量设置
 * 
 * @author sunnymoon
 *
 */
public class HolidayLeaveSettingFragment extends BaseProgressFragment implements OnItemClickListener {

	@Bind(R.id.id_listview)
	protected ListView listview;

	private HolidayLeaveAdapter adapter;

	private List<HolidayLeave> list = new ArrayList<HolidayLeave>();

	@Override
	protected int obtainContentRes() {
		return R.layout.frag_holiday_leave_setting;
	}

	@Override
	protected void initParams() {

	}

	@Override
	protected void initView() {

		listview.setOnItemClickListener(this);

	}

	@Override
	protected void initData() {

		AccessTokenKeeper token = AccessTokenKeeper.readAccessToken(getActivity());
		if (token.isLogin()) {

			RequestParams params = new RequestParams();
			params.put("uid", token.getOpenId());
			ApiRequest.holidayLeaveList.request(this, params);

		}

	}

	public ListView getListview() {
		return listview;
	}

	public List<HolidayLeave> getList() {
		return list;
	}

	public void setAdapter(HolidayLeaveAdapter adapter) {
		this.adapter = adapter;
	}

	public static void go(Activity act) {

		Bundle args = new Bundle();
		MyPageHelper.holidayLeaveSettingFragment.showMyPage(act, args);

	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

		final HolidayLeave holidayLeave = list.get(position);

		DayHourPickerDialogFragment dialogFrag = new DayHourPickerDialogFragment(new DayHourClickListener() {

			@Override
			public void onEnsure(int day, int hour) {

				if (day > 0 || hour > 0) {
					holidayLeave.setLeaveTime(day + "天" + hour + "小时");
					adapter.notifyDataSetChanged();

					AccessTokenKeeper tokenKeeper = AccessTokenKeeper.readAccessToken(getActivity());

					if (tokenKeeper.isLogin()) {

						RequestParams params = new RequestParams();
						params.put("uid", tokenKeeper.getOpenId());
						params.put("to_uid", holidayLeave.getUid());

						int totalHour = day * 8 + hour;
						params.put("leave", totalHour);

						ApiRequest.holidayLeaveSetting.request(HolidayLeaveSettingFragment.this, params);

					}

				}
			}
		});

		dialogFrag.show(getChildFragmentManager(), dialogFrag.getClass().getSimpleName());

	}

}
